package question;

/**
 * @program: javase
 * @description: 三个线程，分别打印1-10，11-20，21-30，要求同时进行
 * ，同时最后需要这些数的和全部加起来打印。即在主线程中进行打印(1-30的和)
 * @author: lld
 * @create: 2021-04-01 19:21
 **/
public class Question1 {
    public static void main(String[] args) {
        Thread thread1=new Thread(()->{
            for (int i = 1; i < 11; i++) {
                System.out.println("线程1："+i);
            }
        });
        Thread thread2=new Thread(()->{
            for (int i = 11; i < 21; i++) {
                System.out.println("线程2："+i);
            }
        });
        Thread thread3=new Thread(()->{
            for (int i = 21; i < 31; i++) {
                System.out.println("线程3："+i);
            }
        });
        //要求三个子线程执行完之后再执行主线程
        thread1.start();
        thread2.start();
        thread3.start();

        try {
//            如在A线程里调用B线程join(),A线程阻塞
            thread1.join();
            thread2.join();
            thread3.join();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        int sum=0;
        for (int i = 1; i < 31; i++) {
            sum=sum+i;
        }
        System.out.println("main线程："+sum);
    }
}
